<?php
namespace Api\Controller;
/**
 * 上传
 */
class UploadController extends ApiController
{
    protected $noCheck = ['segmentupload', 'segmentvideo'];

    function _initialize() {
        parent::_initialize();
   }

   public function segmentvideo(){
        if(empty($_FILES)){
            $this->ajaxReturn(['s'=>false, 'm'=>'请上传文件']);
        }
        $time_limit = ['shop_cof'=>15, 'nearby_people'=>40, 'store'=>60];
        $scene = I('scene', 'shop_cof');
        if(!in_array($scene, array_keys($time_limit))){
            $this->ajaxReturn(['s'=>false, 'm'=>'参数错误']);
        }
        $file_name = I('file_name');
        $blob_num = I('blob_num');
        $total_blob_num = I('total_blob_num');
        if(empty($file_name)){
            $this->ajaxReturn(['s'=>false, 'm'=>'请传入文件名称']);
        }
        if(empty($blob_num)){
            $this->ajaxReturn(['s'=>false, 'm'=>'请传入当前文件包数量']);
        }
        if(empty($total_blob_num)){
            $this->ajaxReturn(['s'=>false, 'm'=>'请传入文件包总量']);
        }
        $file = reset($_FILES);
        require_once 'ThinkPHP/Library/Vendor/autoload.php';
        $obj = new \Huizhang\FileSegmentUpload\FileSegmentUpload();
        $tmp_path = $obj->upload([
            'tmp_name'              => $file['tmp_name'], // 文件内容
            'now_package_num'       => $blob_num, // 当前文件包数量
            'total_package_num'     => $total_blob_num, // 文件包总量
            'file_name'             => $file_name, // 文件名称(唯一)
            'file_path'             => sys_get_temp_dir(), // 文件存放路径
            'clear_interval_time'   => 60, // 清理临时文件间隔，默认五分钟
            'is_continuingly'       => true // 是否断点续传，默认为true
        ]);
        if($tmp_path != 'ongoing'){
            if(is_file($tmp_path)){
                $file = [];
                $file['tmp_name'] = $tmp_path;
                $videoInfo = uploadVideo($file, 1, $time_limit[$scene]);
                if(!$videoInfo['status']){
                    $this->ajaxReturn(['s'=>false, 'm'=>$videoInfo['msg']]);
                }
                $video = C("SiteUrl").$videoInfo['info']['path'];
                $cover = video_cover($video);
                $cover_path = './'.C('UPLOAD_PATH').'/videocover/';
                if(!is_dir($cover_path)){
                    mkdir($cover_path, 0755, true);
                }
                $this->ajaxReturn(['s'=>true, 'm'=>'ok', 'video'=>$video, 'cover'=>$cover]);
            }
            $this->ajaxReturn(['s'=>false, 'm'=>'上传失败']); 
        }
        $this->ajaxReturn(['s'=>false, 'm'=>'上传中']); 
   }
}